package cn.stylefeng.guns.cp;

import cn.hutool.core.net.URLEncoder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.config.BcConfig;
import cn.stylefeng.guns.constant.Constant;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author xiesm
 * @Date 2023/12/22 14:16
 **/
@Slf4j
@RestController
public class CpController {
    @Autowired
    private BcConfig bcConfig;
    private static final Integer timeout = 100000;

    @RequestMapping("/cp/sc")
    public JSONObject sc(String token, String ticketId, String type, Integer num) {
        String url = "";
        switch (type) {
            case "1":
                url = bcConfig.getFt().get(num);
                break;
            case "2":
                url = bcConfig.getSc().get(num);
                break;
            case "3":
                url = bcConfig.getSsq().get(num);
                break;
        }
        Map<String, String> headers = Maps.newHashMap();
        headers.put("Token", token);

        com.alibaba.fastjson.JSONObject params = new com.alibaba.fastjson.JSONObject();
        params.put("ticketId", ticketId);
        params.put("num", 1000);
        String res = HttpUtil.createPost(url)
                .headerMap(headers, true)
                .contentType(ContentType.JSON.toString())
                .body(params.toJSONString())
                .timeout(timeout)
                .execute().body();
        return JSONObject.parseObject(res);
    }

    @RequestMapping("/cp/ao")
    public JSONObject ao(String date, String lotCode) {
        String url = "https://api.api168168.com/pks/getPksHistoryList.do?date={}&lotCode={}";
        url = StrUtil.format(url, date, lotCode);

        String res = HttpUtil.createGet(url)
                .contentType(ContentType.JSON.toString())
                .timeout(timeout)
                .execute().body();
        return JSONObject.parseObject(res);
    }

    @RequestMapping("/cp/save/map")
    public JSONObject save(@RequestBody String requestBody) {
        JSONObject jsonObject = JSONObject.parseObject(requestBody);
        Constant.json.putAll(jsonObject);
        return Constant.json;
    }

    @RequestMapping("/cp/test")
    public JSONObject test() {
        JSONObject result = SsqUtil.getResult();
        List<Ssq> ssqList = SsqUtil.getSsqList(result);
        String maxCode = Ssq.getMaxYilou(ssqList);
        System.out.println(maxCode);
        return Constant.json;
    }

    @RequestMapping("/cp/rmmarket")
    public String rmmarket(Integer type, String gameid, String date) {
        String url = bcConfig.getRm().get(type);
        log.info("rmmarket cookie:{}", Constant.rmCookie[0]);
        switch (type) {
            case 1:
                url += gameid;
                break;
            case 3:
                url += date;
                break;
        }
        log.info("rmmarket url:{}", url);
        String body = HttpUtil.createGet(url)
                .header("Cookie", Constant.rmCookie[0])
                .timeout(timeout)
                .execute().body();
        log.info("rmmarket body:{}", body);
        return body;
    }

    @RequestMapping("/cp/chatShow")
    public String chatShow(String gameId, String marketId, String competitionName,
                           String gameName) {
        String url = bcConfig.getRm().get(2);
        JSONObject params = new JSONObject();
        params.put("gameId", gameId);
        params.put("marketId", marketId);
        params.put("competitionName", competitionName);
        params.put("gameName", gameName);
        log.info("chatShow url:{}", url);
        String body = HttpUtil.createPost(url)
                .header("Cookie", Constant.rmCookie[0])
                .timeout(timeout)
                .form(params)
                .execute().body();
        log.info("chatShow body:{}", body);
        return body;
    }

    @RequestMapping("/cp/fbmarket")
    public String fbmarket(Integer type, String gameid, String current) {
        String url = bcConfig.getFb().get(type);
        log.info("fbmarket url:{}", url);
        JSONObject params = new JSONObject();
        switch (type) {
            case 0:
                params = JSONObject.parseObject(StrUtil.format("{\n" +
                        "    \"languageType\": \"CMN\",\n" +
                        "    \"oddsType\": 1,\n" +
                        "    \"current\": {},\n" +
                        "    \"size\": 100,\n" +
                        "    \"orderBy\": 0,\n" +
                        "    \"isPC\": true,\n" +
                        "    \"sportId\": 1,\n" +
                        "    \"type\": 3\n" +
                        "}", current));
                break;
            case 1:
                params = JSONObject.parseObject(StrUtil.format("{\n" +
                        "    \"languageType\": \"CMN\",\n" +
                        "    \"oddsType\": 1,\n" +
                        "    \"matchId\": \"{}\"\n" +
                        "}", gameid));
                break;
        }
        String body = HttpUtil.createPost(url)
                .body(params.toJSONString())
                .timeout(timeout)
                .execute().body();
        log.info("fbmarket body:{}", body);
        return body;
    }

    public static void main(String[] args) {
        String[] strs = {"1", "2", "3"};
        System.out.println(StrUtil.format("{}{}{}", strs));

    }
}
